﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class DeliveryOrderRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15711;
        protected GridView gvDelivery;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblListTitle;
        protected Label lblMsg;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected Label lblTotalWeight;
        protected Label lblTotalWeightText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalGrossWgt;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2StockList(DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(deliveryOrderRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetDeliveryOrderRptDataSet(deliveryOrderRptQueryEntity);
            base.BindData2GridView(this.gvDelivery, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            deliveryOrderRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = deliveryOrderRptQueryEntity.CurrentPage;
            deliveryOrderRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetDeliveryOrderRptDataSet(deliveryOrderRptQueryEntity))
                {
                    base.Export2Excel("DeliveryOrderRpt.xls", "15711", this.gvDelivery, ds);
                }
            }
            finally
            {
                deliveryOrderRptQueryEntity.IsGetAll = false;
                deliveryOrderRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetDeliveryOrderRptQueryEntity(deliveryOrderRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            deliveryOrderRptQueryEntity.CurrentPage = "0";
            this.BindData2StockList(deliveryOrderRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            deliveryOrderRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(deliveryOrderRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            deliveryOrderRptQueryEntity.DeliveryOrderID = "";
            deliveryOrderRptQueryEntity.ExternOrderId2 = "";
            deliveryOrderRptQueryEntity.OwnerID = "";
            deliveryOrderRptQueryEntity.CustomerID = "";
            deliveryOrderRptQueryEntity.Status = "";
            deliveryOrderRptQueryEntity.Receipted = "";
            deliveryOrderRptQueryEntity.CreatedBy = "";
            deliveryOrderRptQueryEntity.BeginDate = "";
            deliveryOrderRptQueryEntity.EndDate = "";
            this.BindData2StockList(deliveryOrderRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            if (int.Parse(deliveryOrderRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                deliveryOrderRptQueryEntity.CurrentPage = (int.Parse(deliveryOrderRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StockList(deliveryOrderRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            if (int.Parse(deliveryOrderRptQueryEntity.CurrentPage) > 0)
            {
                deliveryOrderRptQueryEntity.CurrentPage = (int.Parse(deliveryOrderRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StockList(deliveryOrderRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
            this.BindData2StockList(deliveryOrderRptQueryEntity);
        }

        private DeliveryOrderRptQueryEntity GetDeliveryOrderRptQueryEntity()
        {
            return (base.PageQueryEntitySession as DeliveryOrderRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
                deliveryOrderRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(deliveryOrderRptQueryEntity);
            }
        }

        protected void gvDelivery_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15711", this.gvDelivery);
        }

        protected void gvDelivery_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    if (((Label) e.Row.FindControl("lblTOTAL_QTY")) != null)
                    {
                        this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblTOTAL_QTY")).Text != "") ? ((Label) e.Row.FindControl("lblTOTAL_QTY")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblTOTAL_WEIGHT")) != null)
                    {
                        this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblTOTAL_WEIGHT")).Text != "") ? ((Label) e.Row.FindControl("lblTOTAL_WEIGHT")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblTOTAL_CUBE")) != null)
                    {
                        this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblTOTAL_CUBE")).Text != "") ? ((Label) e.Row.FindControl("lblTOTAL_CUBE")).Text : "0");
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalWeightText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                if (this.hdnActionID.Value == "EXPORT")
                {
                    for (int i = 0; i < this.gvDelivery.Columns.Count; i++)
                    {
                        if (this.gvDelivery.Columns[i].Visible)
                        {
                            this.gvDelivery.Columns[i].ItemStyle.Reset();
                            this.gvDelivery.Columns[i].ControlStyle.Reset();
                        }
                    }
                }
            }
        }

        protected void gvDelivery_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = this.GetDeliveryOrderRptQueryEntity();
                deliveryOrderRptQueryEntity.SortField = e.SortExpression;
                if ((deliveryOrderRptQueryEntity.SortDirection == "ASC") || (deliveryOrderRptQueryEntity.SortDirection == ""))
                {
                    deliveryOrderRptQueryEntity.SortDirection = "DESC";
                }
                else if (deliveryOrderRptQueryEntity.SortDirection == "DESC")
                {
                    deliveryOrderRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StockList(deliveryOrderRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity = new DeliveryOrderRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    deliveryOrderRptQueryEntity.DeliveryOrderID = this.PreviousPage.DeliveryID;
                    deliveryOrderRptQueryEntity.ExternOrderId2 = this.PreviousPage.ExternOrderID2;
                    deliveryOrderRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    deliveryOrderRptQueryEntity.CustomerID = this.PreviousPage.CustomerID;
                    deliveryOrderRptQueryEntity.Status = this.PreviousPage.Status;
                    deliveryOrderRptQueryEntity.Receipted = this.PreviousPage.Receipted;
                    deliveryOrderRptQueryEntity.CreatedBy = this.PreviousPage.CreatedBy;
                    deliveryOrderRptQueryEntity.BeginDate = this.PreviousPage.BeginDate;
                    deliveryOrderRptQueryEntity.EndDate = this.PreviousPage.EndDate;
                }
                else
                {
                    deliveryOrderRptQueryEntity.DeliveryOrderID = "";
                    deliveryOrderRptQueryEntity.ExternOrderId2 = "";
                    deliveryOrderRptQueryEntity.OwnerID = "";
                    deliveryOrderRptQueryEntity.CustomerID = "";
                    deliveryOrderRptQueryEntity.Status = "";
                    deliveryOrderRptQueryEntity.Receipted = "";
                    deliveryOrderRptQueryEntity.CreatedBy = "";
                    deliveryOrderRptQueryEntity.BeginDate = "";
                    deliveryOrderRptQueryEntity.EndDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            deliveryOrderRptQueryEntity.IsGetAll = false;
            deliveryOrderRptQueryEntity.CurrentPage = "0";
            deliveryOrderRptQueryEntity.PageSize = this.gvDelivery.PageSize.ToString();
            deliveryOrderRptQueryEntity.SortField = "CREATED_DATE,DELIVERY_ORDER_ID,OWNER_ID";
            deliveryOrderRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvDelivery.PageSize;
            this.SetDeliveryOrderRptQueryEntity(deliveryOrderRptQueryEntity);
            this.BindData2StockList(deliveryOrderRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvDelivery);
                this.LoadData();
            }
        }

        public void SetDeliveryOrderRptQueryEntity(DeliveryOrderRptQueryEntity deliveryOrderRptQueryEntity)
        {
            base.PageQueryEntitySession = deliveryOrderRptQueryEntity;
        }

        public DeliveryOrderRptQuery PreviousPage
        {
            get
            {
                return (DeliveryOrderRptQuery) base.PreviousPage;
            }
        }
    }
}

